﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S4Store.Domain.CarKpi;
using LX.Web.Excel;

namespace S4Store.Domain.Report
{
    public class CarSaleCommonReport : ExportObject
    {
        


        #region 数据导出
        public override bool? Discriminate(string propertyname)
        {
            if (propertyname == "OrderRate" ||
                propertyname == "OrderRateDiff")
            {
                return this.OrderRateDiff >= 0;
            }
            if (propertyname == "SaleRate" ||
                propertyname == "SaleRateDiff")
            {
                return this.SaleRateDiff >= 0;
            }
            if (propertyname == "InventoryCoefficient")
            {
                return this.CarKpiMonthConfig.InventoryIndexTarget <= this.InventoryCoefficient;
            }
            if (propertyname == "TBBL" ||
                 propertyname == "TBDiff")
            {
                return this.TBDiff >= 0;
            }
            if (propertyname == "AJBL" ||
                 propertyname == "AJDiff")
            {
                return this.AJDiff >= 0;
            }
            if (propertyname == "YBBL" ||
                 propertyname == "YBDiff")
            {
                return this.YBDiff >= 0;
            }
            if (propertyname == "ZHBL" ||
                 propertyname == "ZHDiff")
            {
                return this.ZHDiff >= 0;
            }
            if (propertyname == "PackageVersionChangeLv")
            {
                return this.PackageVersionChangeLv >= this.CarKpiMonthConfig.PakeageDesignTarget;
            }
            if (propertyname == "PackageXSLV" ||
                 propertyname == "PackageCountDiff" ||
                 propertyname == "PackageDiff")
            {
                return this.PackageDiff >= 0;
            }
            if (propertyname == "DecorateXSLV" ||
                 propertyname == "DecorateDiff" ||
                 propertyname == "DecorateCountDiff")
            {
                return this.DecorateDiff >= 0;
            }
            if (propertyname == "DecorateAmoutOfSingCar" )
            {
                return this.DecorateAmoutOfSingCar >= this.CarKpiMonthConfig.DecorateAvgTagrt;
            }
            return null;
        }
        #endregion


        public int Year { get; set; }
        public int Month { get; set; }
        public int Day { get; set; }
        public CarKpiMonthConfig CarKpiMonthConfig { get; set; }
        public SellerPerformanceStandard SellerPerformanceStandard { get; set; }
        public Store Store { get; set; }

        protected bool showlastday = false;
        public virtual void ShowLastDay(bool value)
        {
            showlastday = value;
        }

        /// <summary>
        /// 本月执行天数
        /// </summary>
        public int Days
        {
            get { return DateTime.DaysInMonth(Year, Month); }
        }

        /// <summary>
        /// 时间进度
        /// </summary>
        public decimal RateOfTime
        {
            get { return (decimal)Day * 100 / (decimal)Days; }
        }


        /// <summary>
        /// 本店交车
        /// </summary>
        [Export("本店交车")]
        public virtual int BDYJ
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 二网交车
        /// </summary>
        [Export("二网交车")]
        public virtual int EWYJ
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 本月交车
        /// </summary>
        [Export("交车合计")]
        public virtual int GiveCarCount
        {
            get { return BDYJ + EWYJ; }
        }

        /// <summary>
        /// 本店已订
        /// </summary>
        [Export("本店已订")]
        public virtual int BDYD
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 二网已订
        /// </summary>
        [Export("二网已订")]
        public virtual int EWYD
        {
            get
            {
                return 0;
            }
        }


        /// <summary>
        /// 本月订单
        /// </summary>
        [Export("累计订车")]
        public virtual int OrderCount
        {
            get { return BDYD + EWYD; }
        }
        /// <summary>
        /// 订单目标
        /// </summary>
        [Export("订单目标")]
        public virtual int OrderTarget
        {
            get
            {
                if (CarKpiMonthConfig.LastMonthReserveDealConversionRate == 0)
                {
                    return 0;
                }
                else
                {
                    return ((decimal)SaleTarget * 100 / CarKpiMonthConfig.LastMonthReserveDealConversionRate).AutoRound1();
                }
            }
        }

        /// <summary>
        /// 订单进度
        /// </summary>
        [Export("订单进度", Format = "{0}%", BackBround = ExcelCellBg.Calculate)]
        public virtual decimal OrderRate
        {
            get
            {
                if (OrderTarget == 0) return RateOfTime;
                return (decimal)OrderCount * 100 / (decimal)OrderTarget;
            }
        }

        /// <summary>
        /// 订单进度差
        /// </summary>
        [Export("订单进度差距", Format = "{0}%", BackBround = ExcelCellBg.Calculate)]
        public virtual decimal OrderRateDiff
        {
            get
            {
                return OrderRate - RateOfTime;
            }
        }

        /// <summary>
        /// 销售目标
        /// </summary>
        [Export("销售目标")]
        public virtual int SaleTarget
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// A类库存
        /// </summary>
        [Export("A类库存")]
        public virtual int AKC
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// B类库存
        /// </summary>
        [Export("B类库存")]
        public virtual int BKC
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// C类库存
        /// </summary>
        [Export("C类库存")]
        public virtual int CKC
        {
            get
            {
                return 0;
            }
        }
        /// <summary>
        /// 在途
        /// </summary>
        [Export("在途")]
        public virtual int ZT
        {
            get
            {
                return 0;
            }
        }
        /// <summary>
        /// C类库龄
        /// </summary>
        [Export("C类库龄")]
        public virtual int CKL
        {
            get
            {
                return 0;
            }
        }
        /// <summary>
        /// 预提
        /// </summary>
        [Export("预提")]
        public virtual int YT
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 当前库存
        /// </summary>
        [Export("库存合计")]
        public virtual int Inventory
        {
            get
            {
                return AKC + BKC + CKC + ZT;
            }
        }

        /// <summary>
        /// 上月库存
        /// </summary>
        [Export("上月库存")]
        public virtual int LastMonthInventory
        {
            get
            {
                return 0;
            }
        }


        /// <summary>
        /// 上月交车
        /// </summary>
        [Export("上月交车")]
        public virtual int LastMonthGiveCar
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 上月库存动态系数
        /// </summary>
        [Export("上月库存系数", BackBround = ExcelCellBg.Calculate)]
        public virtual decimal LastMonthInventoryCoefficient
        {
            get
            {
                if (LastMonthGiveCar == 0 && LastMonthInventory == 0) return 1;
                if (LastMonthGiveCar == 0) return 0;
                return (decimal)LastMonthInventory / (decimal)LastMonthGiveCar;
            }
        }

        /// <summary>
        /// 销量进度
        /// </summary>
        [Export("销售进度", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal SaleRate
        {
            get
            {
                if (SaleTarget == 0)
                {
                    return 0;
                }
                else
                {
                    return (decimal)GiveCarCount * 100 / (decimal)SaleTarget;
                }
            }
        }

        /// <summary>
        /// 销量数量差
        /// </summary>
        public virtual decimal SaleAmountDiff
        {
            get { return GiveCarCount - SaleExpectTarget; }
        }

        /// <summary>
        /// 预期销量
        /// </summary>
        public virtual decimal SaleExpectTarget
        {
            get
            {
                return SaleTarget * RateOfTime / 100;
            }
        }

        /// <summary>
        /// 销量进度差
        /// </summary>
        [Export("销售进度差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal SaleRateDiff
        {
            get { return SaleRate - RateOfTime; }
        }

        /// <summary>
        /// 有望客户合计
        /// </summary>
        [Export("有望客户合计", BackBround = ExcelCellBg.Calculate)]
        public virtual int RegisterCustomerCount
        {
            get
            {
                return HYWKH + AYWKH + BYWKH;
            }
        }

        /// <summary>
        /// 预计登记客户数
        /// </summary>
        [Export("预计登记客户数")]
        public virtual int EstimatedRegisterCustomerCount
        {
            get
            {
                return (int)(RegisterCustomerCount * CarKpiMonthConfig.LastMonthAvgvr / 100);
            }
        }

        /// <summary>
        /// 登记客户数进度差
        /// </summary>
        public virtual int RegisterCustomerDiff
        {
            get { return GiveCarCount + EstimatedRegisterCustomerCount - SaleTarget; }
        }
        /// <summary>
        /// 登记客户进度差
        /// 登记客户进度差率=当月登记客户完成进度-时间进度
        /// 当月登记客户完成进度=首次来店客户登记数/首次来店登记数目标
        /// 首次来店登记数目标=当月销量目标/上月平均成交率
        /// </summary>
        public virtual decimal RegisterCustomerRateDiff
        {
            get
            {
                decimal d = 100;
                if (EstimatedRegisterCustomerCount > 0)
                {
                    d = (decimal)RegisterCustomerCount * 100 / (decimal)EstimatedRegisterCustomerCount;
                }
                return d - RateOfTime;
            }
        }

        /// <summary>
        /// 库存动态系数
        /// </summary>
        [Export("库存系数")]
        public virtual decimal InventoryCoefficient
        {
            get
            {
                if (GiveCarCount == 0 && Inventory == 0) return 1;
                if (GiveCarCount == 0) return Inventory;
                return (decimal)Inventory / (decimal)GiveCarCount;
            }
        }

        /// <summary>
        /// 库存差
        /// 上月-本月
        /// 正数：正常
        /// </summary>
        public virtual decimal InventoryCoefficientDiff
        {
            get
            {
                return LastMonthInventoryCoefficient - InventoryCoefficient;
            }
        }

        /// <summary>
        /// 库存目标
        /// </summary>
        public virtual decimal InventoryTarget
        {
            get
            {
                return SaleTarget * this.CarKpiMonthConfig.InventoryIndexTarget;
            }
        }

        /// <summary>
        /// 库存差
        /// 本月-上月
        /// 负数：正常
        /// </summary>
        public virtual decimal InventoryDiff
        {
            get
            {
                return Inventory - LastMonthInventory;
            }
        }

        /// <summary>
        /// 反库存动态系数
        /// </summary>
        public virtual decimal ReverseInventoryCoefficient
        {
            get { return Inventory == 0 ? 0 : (decimal)GiveCarCount * 100 / (decimal)Inventory; }
        }

        /// <summary>
        /// 已经确认过月初计划
        /// </summary>
        public virtual bool IsCarKpiMonthConfigConfirmed
        {
            get
            {
                return this.CarKpiMonthConfig != null && CarKpiMonthConfig.IsConfirmed;
            }
        }

        /// <summary>
        /// 投保数量
        /// </summary>
        [Export("累计投保")]
        public virtual decimal TBSL
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 投保比例
        /// </summary>
        [Export("投保比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal TBBL
        {
            get
            {
                if (GiveCarCount == 0)
                {
                    return 0;
                }
                else
                {
                    return (decimal)TBSL * 100 / (decimal)GiveCarCount;
                }
            }
        }

        /// <summary>
        /// 投保比例差
        /// </summary>
        [Export("投保比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal TBDiff
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return TBBL - this.CarKpiMonthConfig.InsureTarget;
            }
        }

        /// <summary>
        /// 置换数量
        /// </summary>
        [Export("累计置换")]
        public virtual decimal ZHSL
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 置换比例
        /// </summary>
        [Export("置换比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal ZHBL
        {
            get
            {
                if (GiveCarCount == 0)
                {
                    return 0;
                }
                else
                {
                    return (decimal)ZHSL * 100 / (decimal)GiveCarCount;
                }
            }
        }

        /// <summary>
        /// 置换比例差
        /// </summary>
        [Export("置换比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal ZHDiff
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return ZHBL - this.CarKpiMonthConfig.ReplacementTarget;
            }
        }

        /// <summary>
        /// 按揭数量
        /// </summary>
        [Export("累计按揭")]
        public virtual decimal AJSL
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 按揭比例
        /// </summary>
        [Export("按揭比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal AJBL
        {
            get
            {
                if (GiveCarCount == 0)
                {
                    return 0;
                }
                else
                {
                    return (decimal)AJSL * 100 / (decimal)GiveCarCount;
                }
            }
        }

        /// <summary>
        /// 按揭比例差
        /// </summary>
        [Export("按揭比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal AJDiff
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return AJBL - this.CarKpiMonthConfig.FinancialTarget;
            }
        }

        /// <summary>
        /// 延保数量
        /// </summary>
        [Export("累计延保")]
        public virtual decimal YBSL
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 延保比例
        /// </summary>
        [Export("延保比率", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal YBBL
        {
            get
            {
                if (GiveCarCount == 0)
                {
                    return 0;
                }
                else
                {
                    return (decimal)YBSL * 100 / (decimal)GiveCarCount;
                }
            }
        }

        /// <summary>
        /// 延保比例差
        /// </summary>
        [Export("延保比率差距", BackBround = ExcelCellBg.Calculate, Format = "{0}%")]
        public virtual decimal YBDiff
        {
            get
            {
                if (GiveCarCount == 0) return 0;
                return YBBL - this.CarKpiMonthConfig.ExtendTarget;
            }
        }

        /// <summary>
        /// 标配车型计数
        /// </summary>
        [Export("标配车型计数")]
        public virtual int Bpcxjs
        {
            get { return 1; }
        }

        /// <summary>
        /// 上旬套餐版本交车量
        /// </summary>
        public virtual int LstTenVersionCount
        {
            get { return 0; }
        }

        /// <summary>
        /// 中旬套餐版本交车量
        /// </summary>
        public virtual int MidTenVersionCount
        {
            get { return 0; }
        }

        /// <summary>
        /// 下旬套餐版本交车量
        /// </summary>
        public virtual int LastTenVersionCount
        {
            get { return 0; }
        }

        /// <summary>
        /// 套餐车交车量
        /// </summary>
        [Export("套餐车销售量")]
        public virtual int PackageCount
        {
            get { return LstTenVersionCount + MidTenVersionCount + LastTenVersionCount; }
        }

        /// <summary>
        /// 装潢车交车量
        /// </summary>
        public virtual int DecorateCount
        {
            get { return 0; }
        }

        /// <summary>
        /// 装潢总额
        /// </summary>
        public virtual decimal DecorateAmout
        {
            get { return 0; }
        }

        /// <summary>
        /// 单车装潢总额
        /// </summary>
        [Export("单车装潢总额")]
        public virtual decimal DecorateAmoutOfSingCar
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 套餐车销售率
        /// </summary>
        [Export("套餐车销售率", Format = "{0}%")]
        public virtual decimal PackageXSLV
        {
            get
            {
                if (GiveCarCount == 0)
                {
                    return 0;
                }
                return (decimal)PackageCount * 100 / (decimal)GiveCarCount;
            }
        }

        /// <summary>
        /// 套餐车销售量差距
        /// </summary>
        [Export("套餐车销售量差距")]
        public virtual int PackageCountDiff
        {
            get
            {
                return (int)(PackageCount - GiveCarCount * this.CarKpiMonthConfig.PakeageSaleRateTagrt / 100);
            }
        }

        /// <summary>
        /// 套餐车销售率差
        /// </summary>
        [Export("套餐车销售率差", Format = "{0}%")]
        public virtual decimal PackageDiff
        {
            get
            {
                return PackageXSLV - this.CarKpiMonthConfig.PakeageSaleRateTagrt;
            }
        }

        /// <summary>
        /// 装潢车销售率
        /// </summary>
        [Export("装潢车销售率", Format = "{0}%")]
        public virtual decimal DecorateXSLV
        {
            get
            {
                var count = GiveCarCount - PackageCount;
                if (count <= 0)
                {
                    return 0;
                }
                return (decimal)DecorateCount * 100 / (decimal)count;
            }
        }

        /// <summary>
        /// 装潢车销售率差
        /// </summary>
        [Export("装潢车销售率差", Format = "{0}%")]
        public virtual decimal DecorateDiff
        {
            get
            {
                return DecorateXSLV - this.CarKpiMonthConfig.DecorateSaleRateTagrt;
            }
        }

        /// <summary>
        /// 装潢车销售量差距
        /// </summary>
        [Export("装潢车销售量差距")]
        public virtual int DecorateCountDiff
        {
            get
            {
                return (int)(DecorateCount - (GiveCarCount - PackageCount) * this.CarKpiMonthConfig.DecorateSaleRateTagrt / 100);

            }
        }


        /// <summary>
        /// 套餐车型数
        /// </summary>
        public virtual int PackageCarModelCount
        {
            get
            {
                return 0;
            }
        }


        /// <summary>
        /// 套餐车设计率
        /// </summary>
        public virtual decimal PackageCarSJLV
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// 套餐车设计率数量差距
        /// </summary>
        public virtual int PackageCarSJDiff
        {
            get
            {
                return (int)(PackageCarSJLV - CarKpiMonthConfig.PakeageDesignTarget);
            }
        }


        /// <summary>
        /// 套餐车更换率
        /// </summary>
        [Export("套餐车更换率", Format = "{0}%")]
        public virtual decimal PackageVersionChangeLv
        {
            get
            {
                return 0;
            }
        }

        /// <summary>
        /// H有望客户
        /// </summary>
        [Export("H级有望客户")]
        public virtual int HYWKH
        {
            get { return 0; }
        }

        /// <summary>
        /// A有望客户
        /// </summary>
        [Export("A级有望客户")]
        public virtual int AYWKH
        {
            get { return 0; }
        }
        /// <summary>
        /// B有望客户
        /// </summary>
        [Export("B级有望客户")]
        public virtual int BYWKH
        {
            get { return 0; }
        }

        /// <summary>
        /// H登记客户
        /// </summary>
        [Export("H级登记客户")]
        public virtual int HDJKH
        {
            get { return 0; }
        }

        /// <summary>
        /// A登记客户
        /// </summary>
        [Export("A级登记客户")]
        public virtual int ADJKH
        {
            get { return 0; }
        }
        /// <summary>
        /// B登记客户
        /// </summary>
        [Export("B级登记客户")]
        public virtual int BDJKH
        {
            get { return 0; }
        }

        /// <summary>
        /// 等级客户
        /// </summary>
        [Export("登记客户合计", BackBround = ExcelCellBg.Calculate)]
        public virtual int DJKH
        {
            get { return this.HDJKH + this.ADJKH + this.BDJKH; }
        }

        /// <summary>
        /// 预估订单
        /// </summary>
        public virtual int YGDD
        {
            get { return EstimatedRegisterCustomerCount; }
        }

        /// <summary>
        /// 预估订单差距
        /// </summary>
        public virtual int YGDDCJ
        {
            get
            {
                var target = SaleTarget;
                if (CarKpiMonthConfig.LastMonthReserveDealConversionRate != 0)
                {
                    target = (int)Math.Round((SaleTarget * 100 / CarKpiMonthConfig.LastMonthReserveDealConversionRate), 0, MidpointRounding.AwayFromZero);
                }
                return YGDD + OrderCount - target;
            }
        }

        /// <summary>
        /// 集客缺口
        /// </summary>
        public virtual decimal JKQK
        {
            get
            {
                if (CarKpiMonthConfig.LastMonthAvgvr == 0 || SellerPerformanceStandard.SCLDDJLV == 0) return 0;

                return YGDDCJ * 10000 / CarKpiMonthConfig.LastMonthAvgvr / SellerPerformanceStandard.SCLDDJLV;
            }
        }

        //public virtual List<CarKpiDayData> LastDateData
        //{
        //    get
        //    {
        //        if (CarKpiDayDatas == null || CarKpiDayDatas.Count == 0) return new List<CarKpiDayData>();
        //        var lastdate = CarKpiDayDatas.OrderByDescending(t => t.Date).ToList()[0].Date;
        //        var data = CarKpiDayDatas.Where(t => t.Date.Year == lastdate.Date.Year && t.Date.Month == lastdate.Date.Month && t.Date.Day == lastdate.Date.Day).ToList();
        //        return data;
        //    }
        //}

    }
}
